package com.dcy.znoa.controller.sys;

import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.sys.model.urlList.AddUrlListModel;
import com.dcy.znoa.controller.sys.model.urlList.LogicDelUrlListModel;
import com.dcy.znoa.controller.sys.model.urlList.SelectUrlListModel;
import com.dcy.znoa.controller.sys.model.urlList.UpdateUrlListModel;
import com.dcy.znoa.domain.sys.UrlList;
import com.dcy.znoa.service.sys.UrlListService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;

/**
 * @author chenjie
 */
@Controller
@RequestMapping("urlList")
public class UrlListController extends BaseController {
    @Autowired
    UrlListService urlListService;

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectList")
    public JsonForList selectList(SelectUrlListModel model) throws BusinessException, InvocationTargetException, IllegalAccessException {
        //Oper oper = getCurrentUser();
        return urlListService.selectList(model);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insert")
    public JsonCommon insert(@Valid AddUrlListModel model) throws BusinessException, InvocationTargetException, IllegalAccessException {
//        model.setCreateOperId(getCurrentUser().getOperId());
//        model.setCreateOperName(getCurrentUser().getOperName());
        UrlList urlList = new UrlList();
        BeanUtils.copyProperties(urlList, model);
        return urlListService.insert(urlList);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/updateById")
    public JsonCommon updateById(@Valid UpdateUrlListModel model) throws BusinessException, InvocationTargetException, IllegalAccessException {
//        model.setUpdateOperId(getCurrentUser().getOperId());
    	UrlList urlList = new UrlList();
        BeanUtils.copyProperties(urlList, model);
        return urlListService.updateById(urlList);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/logicDeleteById")
    public JsonCommon logicDeleteById(@Valid LogicDelUrlListModel model) throws BusinessException, InvocationTargetException, IllegalAccessException {
//        model.setUpdateOperId(getCurrentUser().getOperId());
    	UrlList urlList = new UrlList();
        BeanUtils.copyProperties(urlList, model);
        return urlListService.updateById(urlList);
    }   
    
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectUrlListTree")
    //方法：查询所有url，json返回:应该只有系统管理员可用
    public JsonCommon selectUrlListTree() throws BusinessException, InvocationTargetException, IllegalAccessException {      
        return urlListService.selectUrlListTree();
    }
    
    
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectUrlListTreeByRoleId")
    //方法：通过roleId查询urlId：供权限调整时，进行渲染
    public JsonCommon selectUrlListTreeByRoleId(@RequestParam Long roleId) throws BusinessException, InvocationTargetException, IllegalAccessException {      
        return urlListService.selectUrlListTreeByRoleId(roleId);
    }
    
    
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectUrlListByRoleId")
    //方法：通过roleId查询url
    public JsonForList selectUrlListByRoleId(@RequestParam Long roleId) throws BusinessException, InvocationTargetException, IllegalAccessException {      
        return urlListService.selectUrlListByRoleId(roleId);
    }
    
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectUrlListTreeByOperId")
    //方法：通过operId查询url，json返回
    public JsonCommon selectUrlListTreeByOperId(Long operId) throws BusinessException, InvocationTargetException, IllegalAccessException {             
    	if (operId == null) {
    		operId = getCurrentUser().getOperId();
		}
    	return urlListService.selectUrlListTreeByOperId(operId);
    }
}
